<?php
/*
	
*/
defined('BY_JYA') or exit('error');
global $_RQ,$_AR;
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
$return=array();
$tabs=array(
	array('action'=>'/index/','title'=>'基本设置'),
	array('action'=>'/juhelog/','title'=>'短信日志'),
	array('action'=>'/test/','title'=>'前往测试'),
);
if($_RQ['op']=='list'){
	$pindex = max(1, intval($_RQ['page']));
	$psize = 10;
	$params=array('uniacid'=>$_SESSION['uniacid']);
	//$fields=array('id','plugin','tpl_id','status','remark','createtime');
	if(isset($_RQ['status'])){
		$params['status']=intval($_RQ['status']);
		$return['params']['status']=$_RQ['status'];
	}
	//创建时间
	if($_RQ['createtime']){
		$time_range=cfc('tools')->base64_decode($_RQ['createtime'],$_SESSION['base_key_str']);
		$time_range=json_decode($time_range,true);
		$params['createtime between']=array($time_range['tstart'],$time_range['tend']);
		$return['params']['createtime']=$time_range;
	}
	$parameter=array('params'=>$params,'limit'=>array($pindex,$psize),'fields'=>$fields,'orderby'=>'id desc');
	$list=$this->get_list('log',$parameter,$total);
	if($list){
		foreach($list as &$item){
			if($item['plugin']!='core'){
				$item['plugin']=pdo_getcolumn('core_plugins',array('name'=>$item['plugin']),'title');
			}else{
				$item['plugin']='系统';
			}
		}
	}
	$return['list']=$list?$list:array();
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$return['htmls']=array (
		  'type' => 'list',
		  'export'=>'export',
		  'tabs'=>$tabs,
		  'list' => 
		  array (
		    'data' => 'list','edit'=>false,
		    'list' => 
		    array (
		      array (
		        'type' => 'string',
		        'name' => 'id',
		        'title' => 'ID',
		      ),
		      array (
		        'name' => 'plugin',
		        'type' => 'string',
		        'title' => '调用插件',
		      ),
			  array (
		        'name' => 'mobile',
		        'type' => 'string',
		        'title' => '手机号',
		      ),
		      array (
		        'name' => 'tpl_id',
		        'type' => 'string',
		        'title' => '模板id',
		      ),
		      array (
		        'name' => 'status',
		        'type' => 'radio',
		        'title' => '发送状态',
		        'options' => 'status_xuanxiang',
		      ),
		      array (
		        'name' => 'remark',
		        'type' => 'string',
		        'title' => '详情',
		      ),
		      array (
		        'type' => 'time',
		        'name' => 'createtime',
		        'title' => '创建时间',
		      ),
		    ),
		  ),
		  'search'=>array(
				array('name'=>'mobile','width'=>10,'title'=>'手机号','type'=>'string'),
				array('name'=>'status','width'=>10,'title'=>'发送状态','type'=>'radio','options'=>'status_xuanxiang'),
				array('name'=>'createtime','width'=>15,'title'=>'发送时间','type'=>'time_range'),
			)
		);
	$return['status_xuanxiang']=array (
	  0 => '失败',
	  1 => '成功',
	);
	exi($return);
}
if($_RQ['op']=='edit'){
	exi('无效访问！','error');
	$id=intval($_RQ['id']);
	if($id){
		$item=$this->get('log',array('id'=>$id));
	}else{
		$item=array();
	}
	$return['item']=$item;
	$return['htmls']=array (
	  'type' => 'edit',
	  'post' => 'post',
	  'edit_tab' => NULL,
	  'edits' => 
		array (
		  ),
	);
	exi($return);
}
if($_RQ['op']=='post'){
	exi('无效访问！','error');
	$post=get_postdata();
	$data=array_fields(array(),$post);
	$id=intval($post['id']);
	if($id){
		$this->update('log',$data,array('id'=>$id));
		exi('修改日志成功！');
	}else{
		$data['uniacid']=$_SESSION['uniacid'];
		$data['createtime']=TIMESTAMP;
		$this->insert('log',$data);
		exi('新增日志成功！');
	}
}
if($_RQ['op']=='export'){
	if(!empty($_RQ['ordersn'])){
		$params['ordersn']=trim($_RQ['ordersn']);
	}
	if(!empty($_RQ['status'])){
		$params['status']=intval($_RQ['status']);
	}
	//创建时间
	if($_RQ['createtime']){
		$time_range=cfc('tools')->base64_decode($_RQ['createtime'],$_SESSION['base_key_str']);
		$time_range=json_decode($time_range,true);
		$params['createtime between']=array($time_range['tstart'],$time_range['tend']);
	}
	$params['uniacid']=$_SESSION['uniacid'];
	$logs=$this->pdo_getall('log',$params);
	$status_xuanxiang=array (
	  0 => '失败',
	  1 => '成功',
	);
	foreach($logs as &$v){
		if($v['plugin']!='core'){
			$v['plugin']=pdo_getcolumn('core_plugins',array('name'=>$v['plugin']),'title');
		}else{
			$v['plugin']='系统';
		}
		$v['createtime']=date('Y-m-d H:i:s',$v['createtime']);
	}
	$header=array(
		'id'=>'ID',
		'plugin'=>'所属插件',
		'mobile'=>'手机号',
		'tpl_id'=>'模板id',
		'price'=>'发送状态',
		'remark'=>'详情',
		'createtime'=>'创建时间',
	);
	cfc('tools')->export2excel($header,$logs,'短信日志');
}
if($_RQ['op']=='del'){
	$id=intval($_RQ['id']);
	if($id){
		$this->delete('log',array('id'=>$id));
	}
	$del=trim($_RQ['del'],';');
	if($del){
		$del=explode(';',$del);
		foreach($del as $id){
			$id=intval($id);
			if($id){
				$this->delete('log',array('id'=>$id));
			}
		}
	}
	exi('删除成功！');
}